package com.Txunda.fxw.ui.activity.other;

import java.util.Timer;
import java.util.TimerTask;

import com.Txunda.fxw.R;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.toolkit.util.DensityUtil;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCSlideView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 * 企业详情界面
 * @author Administrator
 *
 */
public class EnterpriseAty extends BaseAty {

	@ViewInject(R.id.aty_title3_tv_center)
	private TextView aty_title3_tv_center;
	@ViewInject(R.id.aty_title3_tv_right2)
	private TextView aty_title3_tv_right2;
	@ViewInject(R.id.enterprise_recycler)
	private RecyclerView enterprise_recycler;
	private MyAdapter adapter;
	@ViewInject(R.id.icon_logo)
	private ImageView icon_logo;

	// 屏幕宽度
	private int mScreenWidth = 0;
	private XCSlideView mSlideViewLeft;
	private XCSlideView mSlideViewRight;
	private Context mContext;
	private LinearLayout leave_linear;
	private LinearLayout linear_share;
	private float mScreenHeight;
	private LinearLayout linear_top;
	private LinearLayout linear_bottom;
	private LinearLayout linear_share2;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
				case 1:
					linear_top.setVisibility(View.VISIBLE);
					linear_bottom.setVisibility(View.GONE);
					ValueAnimator animator2 = ValueAnimator.ofFloat(0, mScreenHeight);
					animator2.setTarget(linear_bottom);
					animator2.setDuration(1000).start();
					animator2.addUpdateListener(new AnimatorUpdateListener() {
						@Override
						public void onAnimationUpdate(ValueAnimator animation) {
							linear_bottom.setTranslationY((Float) animation.getAnimatedValue());
						}
					});
					break;

				default:
					break;
			}

		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected int getLayoutResId() {
		return R.layout.aty_enterprise;
	}

	@Override
	@OnClick({R.id.icon_logo,R.id.enterpise_address,R.id.enterprise_speicial})
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
			case R.id.icon_logo:
				if (!mSlideViewRight.isShow())
					mSlideViewRight.show();

				break;
			case R.id.enterpise_address:
				startActivity(PathAty.class,null);
				break;
			case R.id.enterprise_speicial:   //特色
				startActivity(FeaturesAty.class,null);
				break;

			default:
				break;
		}
	}

	@Override
	protected void initialized() {
		String area_name = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
		aty_title3_tv_center.setText("静茶茶业有限公司");
		aty_title3_tv_right2.setVisibility(View.VISIBLE);
		aty_title3_tv_right2.setText(area_name);
		// 设置LinearLayoutManager
		enterprise_recycler.setLayoutManager(
				new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, true));
		enterprise_recycler.setItemAnimator(new DefaultItemAnimator());
		// 设置固定大小
		enterprise_recycler.setHasFixedSize(true);
		adapter = new MyAdapter();
		enterprise_recycler.setAdapter(adapter);

		//================侧滑加动画a==============================
		// 侧滑
		mContext = this;
		mScreenWidth = DensityUtil.getScreenWidthAndHeight(mContext)[0];
		mSlideViewRight = XCSlideView.create(EnterpriseAty.this, XCSlideView.Positon.RIGHT);
		View menuViewRight = LayoutInflater.from(mContext).inflate(R.layout.layout_slide, null);
		mSlideViewRight.setMenuView(EnterpriseAty.this, menuViewRight);
		mSlideViewRight.setMenuWidth(mScreenWidth * 3 / 9);

		leave_linear = (LinearLayout) menuViewRight.findViewById(R.id.linear_leave); // 评价
		linear_share = (LinearLayout) menuViewRight.findViewById(R.id.linear_share); // 分享
		linear_top = (LinearLayout) menuViewRight.findViewById(R.id.linear_top);
		linear_bottom = (LinearLayout) menuViewRight.findViewById(R.id.linear_bottom);
		linear_share2 = (LinearLayout) menuViewRight.findViewById(R.id.linear_share2);
		// 分享
		linear_share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 向上走的动画
				if (linear_top.getVisibility() == View.VISIBLE) {

					linear_top.setVisibility(View.GONE);
					linear_bottom.setVisibility(View.VISIBLE);
					ValueAnimator animator = ValueAnimator.ofFloat(mScreenHeight / 2, 0);
					animator.setTarget(linear_bottom);
					animator.setDuration(500).start();
					animator.addUpdateListener(new AnimatorUpdateListener() {
						@Override
						public void onAnimationUpdate(ValueAnimator animation) {
							linear_bottom.setTranslationY((Float) animation.getAnimatedValue());
						}
					});
				}

			}
		});
		linear_share2.setOnClickListener(new OnClickListener() {
			private Timer timer;
			private TimerTask timetask;
			@Override
			public void onClick(View v) {

				if (linear_top.getVisibility() == View.GONE) {
					linear_top.setVisibility(View.VISIBLE);
					linear_bottom.setVisibility(View.GONE);
					float start = 0 - mScreenHeight;
					ValueAnimator animator = ValueAnimator.ofFloat(start, 0);
					animator.setTarget(linear_top);
					animator.setDuration(500).start();
					animator.addUpdateListener(new AnimatorUpdateListener() {
						@Override
						public void onAnimationUpdate(ValueAnimator animation) {
							linear_top.setTranslationY((Float) animation.getAnimatedValue());
						}
					});
					timer = new Timer();
					timetask = new TimerTask() {

						@Override
						public void run() {
							Message msg = new Message();
							msg.what = 1;
							handler.sendMessage(msg);
						}
					};
					timer.schedule(timetask, 300);

				}

			}
		});
		//=============================================
	}

	@Override
	protected void requestData() {

	}

	/**
	 * RecyclerView的adapter
	 */
	public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

		public class MyViewHolder extends RecyclerView.ViewHolder {

			private ImageView tv_detail_img;
			private TextView tv_detail_title;

			public MyViewHolder(View itemView) {
				super(itemView);
				tv_detail_img = (ImageView) itemView.findViewById(R.id.tv_detail_img);
				tv_detail_title = (TextView) itemView.findViewById(R.id.tv_detail_title);
			}

		}

		@Override
		public int getItemCount() {
			return 6;
		}

		@Override
		public void onBindViewHolder(MyViewHolder arg0, int arg1) {
			// 给ViewHolder设置元素
			arg0.tv_detail_img.setImageResource(R.drawable.icon_bookstore);
			arg0.tv_detail_title.setText("书店");

		}

		@Override
		public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
			View view = LayoutInflater.from(arg0.getContext()).inflate(R.layout.item_layout_detail, null);
			MyViewHolder holder = new MyViewHolder(view);
			return holder;
		}
	}


}
